<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript" src="js/jquery-3.5.1.js"></script>
</head>
<body>
<div>前后端交互</div>
<script type="text/javascript">
    /*
        前后端交互-异步编程与promise概述
     */
    // console.log(typeof Promise);
    // console.dir(Promise)
    function queryData(url) {
        var p = new Promise(function (resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState !== 4) return;
                if(xhr.readyState === 4 && xhr.status === 200){
                    resolve(xhr.responseText);
                }else{
                    reject('服务器错误');
                }
            };
            xhr.open('get', url);
            xhr.send(null)
        });
        return p;
    }
    // queryData('http://localhost:3000/data')
    // .then(function (data) {
    //     console.log(data);
    // },function (info) {
    //     console.log(info);
    // });
    queryData('http://localhost:3000/data')
    .then(function (data) {
        console.log(data)
        return queryData('http://localhost:3000/data1')
    })
    .then(function (data) {
        console.log(data)
        return queryData('http://localhost:3000/data2')
    })
    .then(function (data) {
        console.log(data)
    })
</script>
</body>
</html>